﻿using WeifenLuo.WinFormsUI.Docking;
using G.Model.Runtime;
using GameLibrary.Model;
using GameControls;

namespace GameDesigner2
{
    public partial class PropertyForm : DockContent
    {
        private GameDisplayer _GameDisplayer;
        
        public PropertyForm()
        {
            InitializeComponent();
            ActiveGame.GameChangedEvent += OnGameSelected;
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
            ActiveGame.GameChangedEvent -= OnGameSelected;
        }
        
        private void OnGameSelected(GameDisplayer game)
        {
            if (game == null)
            {
                this.propertyGrid1.SelectedObject = null;
                if (_GameDisplayer != null)
                {
                    _GameDisplayer.ActiveObject.SelectedEvent -= OnChange;
                }
            }
            else
            {
                _GameDisplayer = game;
                _GameDisplayer.ActiveObject.SelectedEvent += OnChange;
            }
        }
        
        private void OnChange(object obj)
        {
            this.propertyGrid1.SelectedObject = obj;
        }
    }
}
